package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Clas;
import com.zyg.service.ClasService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/clas")
public class ClasController {
    @Autowired
    private ClasService clasService;
    @RequestMapping("/tolist")
    public String tolist(){
        return "clas/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public String list(Clas clas, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        // 查询数据
        LayData layData = new LayData();
        QueryWrapper<Clas> qw = new QueryWrapper<>();
        if (clas.getClasName()!=null&&!"".equals(clas.getClasName())){
            //模糊查询
            qw.like("clas_name",clas.getClasName());
        }
        long count = clasService.count(qw);
        layData.setCount((int) count);
        //查询list
        Page<Clas> page1=new Page<>(page,limit);
        List<Clas> list =(clasService.list(page1, qw));
        layData.setData(list);
        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);
        return data;
    }
    @RequestMapping("/toadd")
    public String toAdd(){
        return "class/add";
    }
    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Clas clas){
        boolean flag = clasService.save(clas);
        if (flag){
            return JSON.toJSONString(new LayData(0,"添加成功"));
        }else{
            return JSON.toJSONString(new LayData(500,"添加失败"));
        }
    }
    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model){
        Clas clas = clasService.getById(id);
        model.addAttribute("clas", clas);
        return "clas/update";
    }
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Clas clas){
        boolean flag = clasService.updateById(clas);
        if (flag){
            return JSON.toJSONString(new LayData(0,"修改成功"));
        }else {
            return JSON.toJSONString(new LayData(500,"修改失败"));
        }
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String deleteClas(@PathVariable("id") int id){
        boolean flag = clasService.removeById(id);
        if (flag){
            return JSON.toJSONString(new LayData(0,"删除成功"));
        }else{
            return JSON.toJSONString(new LayData(500,"删除失败"));
        }
    }
}
